import { defineStore } from 'pinia';
import { getAppSetting, setAppSetting } from '../tauriApi';

export const useEditorStore = defineStore('editor', {
  state: () => ({
    activeFile: null as { path: string; content: string | null } | null,
    fontSize: 13 as number,
    wordWrap: 'off' as 'off'|'on'|'bounded',
    minimapEnabled: false as boolean,
    renderWhitespace: 'selection' as 'none'|'selection'|'all',
    tabSize: 2 as number,
    insertSpaces: true as boolean,
    readOnly: false as boolean,
    initialized: false as boolean,
  }),
  actions: {
    setActiveFile(path: string | null, content: string | null = null) {
        if (path === null) {
            this.activeFile = null;
        } else {
            this.activeFile = { path, content };
        }
    },
    setActiveFileContent(content: string) {
        if (this.activeFile) {
            this.activeFile.content = content;
        }
    },
    async initFromSettings() {
      if (this.initialized) return;
      try { const v = await getAppSetting('editor.fontSize'); if (v) this.fontSize = Number(v) || 13; } catch {}
      try { const v = await getAppSetting('editor.wordWrap'); if (v) this.wordWrap = String(v) as any; } catch {}
      try { const v = await getAppSetting('editor.minimap'); if (v!=null) this.minimapEnabled = (v==='1'||v==='true'); } catch {}
      try { const v = await getAppSetting('editor.renderWhitespace'); if (v) this.renderWhitespace = String(v) as any; } catch {}
      try { const v = await getAppSetting('editor.tabSize'); if (v) this.tabSize = Number(v) || 2; } catch {}
      try { const v = await getAppSetting('editor.insertSpaces'); if (v!=null) this.insertSpaces = (v==='1'||v==='true'); } catch {}
      try {
        const v = await getAppSetting('editor.readOnly');
        if (v==null) { this.readOnly = false; }
        else this.readOnly = (v==='1'||v==='true');
      } catch { this.readOnly = false; }
      this.initialized = true;
    },
    async setFontSize(size: number) {
      this.fontSize = size;
      try { await setAppSetting('editor.fontSize', String(size)); } catch {}
    },
    async setWordWrap(mode: 'off'|'on'|'bounded') {
      this.wordWrap = mode;
      try { await setAppSetting('editor.wordWrap', String(mode)); } catch {}
    },
    async setMinimapEnabled(v: boolean) {
      this.minimapEnabled = !!v;
      try { await setAppSetting('editor.minimap', v ? '1' : '0'); } catch {}
    },
    async setRenderWhitespace(v: 'none'|'selection'|'all') {
      this.renderWhitespace = v;
      try { await setAppSetting('editor.renderWhitespace', String(v)); } catch {}
    },
    async setTabSize(size: number) {
      this.tabSize = size;
      try { await setAppSetting('editor.tabSize', String(size)); } catch {}
    },
    async setInsertSpaces(v: boolean) {
      this.insertSpaces = !!v;
      try { await setAppSetting('editor.insertSpaces', v ? '1' : '0'); } catch {}
    },
    async setReadOnly(v: boolean) {
      this.readOnly = !!v;
      try { await setAppSetting('editor.readOnly', v ? '1' : '0'); } catch {}
    },
  },
});

export interface EditorGroup {
  id: string;
  tabs: string[]; // 改为 string[] 而不是 EditorTab[]
  activeTabId: string | null;
  splitDirection?: 'horizontal' | 'vertical';
}


